package com.kedsoft.docms.service.impl;

import com.kedsoft.docms.entity.TableInfo;
import com.kedsoft.docms.mapper.TableMapper;
import com.kedsoft.docms.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/20.
 */
@Service
public class TableServiceImpl implements TableService {

    @Autowired
    private TableMapper tableMapper;

    @Override
    public List<TableInfo> getList() {
        List<TableInfo> list = tableMapper.listTable();
        List<TableInfo> result = new ArrayList<>();
        int id = 1;
        for (TableInfo t:list) {
            t.setId(id);
            result.add(t);
            id++;
        }
        return result;
    }

    public String getTableDDL(String table,String type){
        String cmd = "show create table "+table;
        List<Map> list = tableMapper.getTableDDL(cmd);
        String ddl = "";
        if(type.equals("html")){
            for (Map<String, String> map : list) {
                ddl = map.get("Create Table");
            }
            String sql = "DROP TABLE IF EXISTS " + table + ";<br>";
            sql += ddl + "<br><br>";
            return sql;
        }else {
            for (Map<String, String> map : list) {
                ddl = map.get("Create Table");
            }
            String sql = "DROP TABLE IF EXISTS " + table + ";\n";
            sql += ddl + "\n\n";
            return sql;
        }
    }
}
